package com.wzz.seckillone.controller;


import com.alibaba.fastjson.JSON;
import com.wzz.seckillone.common.AjaxResult;
import com.wzz.seckillone.common.Common;
import com.wzz.seckillone.pojo.SeckillOrder;
import com.wzz.seckillone.pojo.User;
import com.wzz.seckillone.service.SeckillOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-11-08
 */
@Controller
@RequestMapping("/secKillOrder")
@Slf4j
public class SecKillOrderController {


    @Autowired
    private Common common;
    @Autowired
    private SeckillOrderService seckillOrderService;

    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 跳转到订单页面 携带orderId
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/21 21:16
     */
    @GetMapping("/toOrderDetail")
    public String toOrderDetail(String orderId, Model model){
        model.addAttribute("orderId",orderId);
        log.info("orderDetail"+orderId);
        return "orderDetailPlus";
    }
    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 跳转到 秒杀失败页面
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/22 16:03
     */
    @GetMapping("/toFailSecKillOrder")
    public String toFailSecKillOrder(String orderId,Model model){
        model.addAttribute("orderId",orderId);
        return "failSecKill";
    }
    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 返回结果 -1； 秒杀失败 0排队中；1返回一个用户只能购买一个;否则成功
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/22 15:15
     */
    @GetMapping("/getResult")
    @ResponseBody
    public String getResult(String goodsId){
        AjaxResult result = new AjaxResult();
        try{
            User user = common.getUser();
            result.setSuccess(true).setData(seckillOrderService.getResult(user,goodsId));
        }catch(Exception e){
            result.setSuccess(false).setMessage("查询失败");
        }
        return JSON.toJSONString(result);
    }

    @GetMapping("/detail")
    @ResponseBody
    public String getDetail(String orderId){
        AjaxResult result = new AjaxResult();
        try{
            result.setSuccess(true).setData(seckillOrderService.getOrderDetailsByOrderId(orderId));
        }catch (Exception e){
            result.setSuccess(false).setMessage("查询失败");
        }
        return JSON.toJSONString(result);
    }
}
